import store from "@/store";
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/pages/HomePage.vue";
import Login from "@/pages/Login.vue";

Vue.use(VueRouter);
const router = new VueRouter({
    mode: "hash",
    base: process.env.BASE_URL,
    routes: [
        {
            path: "/",
            name: "Login",
            component: Login,   
        },
        {
            path: "/home",
            name: "Home",
            component: Home,
        },
    ],
});

//全局前置守卫
router.beforeEach((to,from,next) =>{
    const applySuccess = store.state.applySuccess;
    if (!applySuccess && to.path !== '/') {
        // 没有申请，只能访问根目录
        next('/');
        
    } else {
        // 已经申请资源，想去哪就去哪
        next();
    }
});

export default router;
